package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车是否已经存在这个商品，存在只需要加一
        //不存在就需要插入新数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart select = shoppingCartMapper.select(shoppingCart);
        if(select!=null){
            //当前用户菜品已经存在
            select.setNumber(select.getNumber()+1);
            shoppingCartMapper.updateData(select);
        }
        else {
            if (shoppingCartDTO.getSetmealId() == null) {
                //添加的是菜品
                DishVO dishVO = dishService.select(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
            } else {
                //添加的是套餐
                SetmealVO setmealVO = setmealService.selectSetmealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    //查看购物车
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list = shoppingCartMapper.list(BaseContext.getCurrentId());
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.cleanShoppingCart(BaseContext.getCurrentId());
    }

    @Override
    public void subtract(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //如果该商品数量只有1，直接删除，大于1，减少数量
        ShoppingCart select = shoppingCartMapper.select(shoppingCart);
        if(select.getNumber()==1){
        shoppingCartMapper.substract(shoppingCart);
        }
        else {
            select.setNumber(select.getNumber()-1);
            shoppingCartMapper.updateData(select);
        }
    }
}
